package com.hgyc.mom.system.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.system.entity.SysWebToken;
import com.hgyc.mom.system.vo.SysWebTokenParam;
import com.hgyc.mom.system.service.SysWebTokenService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * Web令牌 Controller
 *
 * @author fwj
 * @since 2025-08-08
 */
@Tag(name = "Web令牌 控制器", description = "Web令牌 控制器")
@RestController
@RequestMapping("/system/token")
public class SysWebTokenController {
    @Autowired
    private SysWebTokenService sysWebTokenService;

    @Operation(summary = "查询Web令牌 列表", description = "查询Web令牌 列表")
    @PostMapping("/pageList")
    public Result<IPage<SysWebToken>> pageList(@Parameter(description = "查询参数") @RequestBody SysWebTokenParam sysWebTokenParam) {
        Page<SysWebToken> page = new Page<>(sysWebTokenParam.getPage(), sysWebTokenParam.getPageSize());
        SysWebToken sysWebToken = new SysWebToken();
        BeanUtils.copyProperties(sysWebTokenParam, sysWebToken);
        QueryWrapper<SysWebToken> wrapper = QueryWrapperUtil.build(sysWebToken);
        IPage<SysWebToken> list = sysWebTokenService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取Web令牌 详细信息", description = "获取Web令牌 详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "Web令牌 ID") @PathVariable("id") Long id) {
        return Result.success(sysWebTokenService.getById(id));
    }
    @Operation(summary = "新增Web令牌 ", description = "新增Web令牌 ")
    @PostMapping
    public Result<?> save(@Parameter(description = "Web令牌 ") @RequestBody SysWebToken sysWebToken) {
        sysWebTokenService.saveToken(sysWebToken);
        return Result.success();
    }

    @Operation(summary = "修改Web令牌 ", description = "修改Web令牌 ")
    @PutMapping
    public Result<?> update(@Parameter(description = "Web令牌 ") @RequestBody SysWebToken sysWebToken) {
        sysWebTokenService.updateToken(sysWebToken);
        return Result.success();
    }

    @Operation(summary = "删除Web令牌 ", description = "删除Web令牌 ")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(sysWebTokenService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "新增Web令牌 ", description = "新增Web令牌 ")
    @PostMapping("/login")
    public Result<?> login(@Parameter(description = "Web令牌 ") @RequestBody SysWebToken sysWebToken) {
        sysWebTokenService.login(sysWebToken);
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SysWebToken> util = new ExcelUtil<>(SysWebToken.class);
        util.importTemplateExcel(response, "Web令牌 ");
    }

    @Operation(summary = "导出Web令牌 列表", description = "导出Web令牌 列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysWebTokenParam sysWebTokenParam) {
        SysWebToken sysWebToken = new SysWebToken();
        BeanUtils.copyProperties(sysWebTokenParam, sysWebToken);
        QueryWrapper<SysWebToken> wrapper = QueryWrapperUtil.build(sysWebToken);
        List<SysWebToken> list = sysWebTokenService.list(wrapper);
        ExcelUtil<SysWebToken> util = new ExcelUtil<>(SysWebToken.class);
        util.exportExcel(response, list, "Web令牌 数据");
    }

    @Operation(summary = "导入Web令牌 列表", description = "导入Web令牌 列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SysWebToken> util = new ExcelUtil<>(SysWebToken.class);
        List<SysWebToken> sysWebTokenList = util.importExcel(file.getInputStream());
        String message = sysWebTokenService.importData(sysWebTokenList, updateSupport);
        return Result.success(message);
    }
}
